import { getCategoriesList } from '@/api/category'
import { goodsList } from '@/api/goods.js'

const state = {
  cateList: [],
  cateListTotal: 0,
  goodslist: [],
  goodsListTotal: 0
}

const mutations = {
  CHANG_CATELIST(state, cateList) {
    state.cateList = cateList
  },
  CHANG_TOTAL(state, total) {
    state.cateListTotal = total
  },
  CHANG_GOODSLIST(state, goodslist) {
    state.goodslist = goodslist
  },
  CHANG_TOTAL(state, total) {
    state.goodsListTotal = total
  }
}

const actions = {
  async changCateList({commit}, queryInfo) {
    const data = await getCategoriesList(queryInfo)
    if (data.meta.status !== 200) return this.$message.error('获取分类列表失败')
    commit('CHANG_CATELIST', data.data.result)
    commit('CHANG_TOTAL', data.data.total)
  },
  async changGoodsList({commit}, queryInfo) {
    const data = await goodsList(queryInfo)
    if (data.meta.status !== 200) return this.$message.error('获取商品数据失败')
    commit('CHANG_GOODSLIST', data.data.goods)
    commit('CHANG_TOTAL', data.data.total)
  }
}

export default {
  state,
  actions,
  mutations
}